package cn.cq.admin.controller;

import cn.cq.admin.entity.dto.BatchRemove;
import cn.cq.admin.entity.dto.CourseDto;
import cn.cq.admin.entity.model.Course;
import cn.cq.admin.entity.vo.SelectVo;
import cn.cq.admin.service.CourseService;
import cn.cq.admin.entity.vo.CourseVo;
import cn.cq.admin.util.VoCast;
import cn.cq.common.BaseConst;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping("/doList")
    public SaResult doList(@RequestBody CourseDto courseDto) {
        Page<Course> coursePage = courseService.doList(courseDto);
        List<CourseVo> collect = coursePage.getRecords().stream().map(VoCast::convertToCourseVo).collect(Collectors.toList());
        Page<CourseVo> courseVoPage = new Page<>();
        courseVoPage.setRecords(collect);
        courseVoPage.setTotal(coursePage.getTotal());
        courseVoPage.setSize(coursePage.getSize());
        courseVoPage.setCurrent(coursePage.getCurrent());
        return SaResult.data(courseVoPage);
    }

    @PostMapping("/doSave")
    public SaResult doSave(@RequestBody Course course) {
        courseService.save(course);
        return SaResult.ok("新增成功");
    }
    @PutMapping("/doModify")
    public SaResult doModify(@RequestBody Course course) {
        courseService.updateById(course);
        return SaResult.ok("修改成功");
    }
    @DeleteMapping("/doRemove")
    public SaResult doDelete(@RequestBody BatchRemove batchRemove) {
        courseService.removeBatchByIds(batchRemove.getIds());
        return SaResult.ok("删除成功");
    }
    @GetMapping("/doListAll")
    public SaResult doListAll() {
        List<Course> list = courseService.lambdaQuery().eq(Course::getStatus, BaseConst.STATUS_ENABLE).list();
        List<SelectVo> collect = list.stream().map(course -> {
            SelectVo selectVo = new SelectVo();
            selectVo.setValue(String.valueOf(course.getId()));
            selectVo.setLabel(course.getCourseName());
            return selectVo;
        }).collect(Collectors.toList());
        return SaResult.data(collect);
    }



}
